/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.googlecode.midpdeviceprofiler;

import com.googlecode.midpdeviceprofiler.SupportedApisView;
import com.googlecode.midpdeviceprofiler.MainMenuView;
import com.googlecode.midpdeviceprofiler.MidpProfileView;
import com.googlecode.midpdeviceprofiler.RamMemoryView;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.*;

/**
 * @author helio.rodrigues
 */
public class DeviceProfilerMidlet extends MIDlet implements CommandListener {

    private static DeviceProfilerMidlet instance;

    private MainMenuView mainMenuView;

    private MidpProfileView midpProfileView;

    private SupportedApisView supportedApisView;

    private RamMemoryView ramMemoryView;

    /**
     * @return the instance
     */
    public static DeviceProfilerMidlet getInstance() {
        return instance;
    }

    /**
     *
     */
    public void startApp() {
        instance = this;
        showDisplay(getMainMenuView());
    }

    /**
     *
     * @param displayable
     */
    private void showDisplay(Displayable displayable) {
        Display.getDisplay(this).setCurrent(displayable);
    }

    /**
     *
     */
    public void pauseApp() {
        notifyPaused();
    }

    /**
     *
     * @param unconditional
     */
    public void destroyApp(boolean unconditional) {
        notifyDestroyed();
    }

    /**
     *
     * @param command
     * @param displayable
     */
    public void commandAction(Command command, Displayable displayable) {
        // Main menu commands.
        if (displayable == getMainMenuView()) {
            if (command == getMainMenuView().getExitCommand()) {
                destroyApp(true);
            } else if (command == MainMenuView.SELECT_COMMAND) {
                if (getMainMenuView().isSelected(0)) {
                    showDisplay(getMidpProfileView());
                }

                if (getMainMenuView().isSelected(1)) {
                    showDisplay(getSupportedApisView());
                }

                if (getMainMenuView().isSelected(2)) {
                    showDisplay(getRamMemoryView());
                }
            }
        }

        // Supported API's commands.
        if (displayable == getSupportedApisView()) {
            if (command == getSupportedApisView().getBackCommand()) {
                showDisplay(getMainMenuView());
            }
        }

        // RAM Memory commands.
        if (displayable == getRamMemoryView()) {
            if (command == getRamMemoryView().getBackCommand()) {
                showDisplay(getMainMenuView());
            }
        }

        // MIDP profile commands.
        if (displayable == getMidpProfileView()) {
            if (command == getMidpProfileView().getBackCommand()) {
                showDisplay(getMainMenuView());
            }
        }
    }

    /**
     * @return the mainMenuList
     */
    public MainMenuView getMainMenuView() {
        if (mainMenuView == null) {
            mainMenuView = new MainMenuView();
        }

        return mainMenuView;
    }

    /**
     * @return the profileForm
     */
    public SupportedApisView getSupportedApisView() {
        if (supportedApisView == null) {
            supportedApisView = new SupportedApisView();
        }

        return supportedApisView;
    }

    /**
     * @return the ramMemoryView
     */
    public RamMemoryView getRamMemoryView() {
        if (ramMemoryView == null) {
            ramMemoryView = new RamMemoryView();
        }

        return ramMemoryView;
    }

    /**
     * @return the midpProfileView
     */
    public MidpProfileView getMidpProfileView() {
        if (midpProfileView == null) {
            midpProfileView = new MidpProfileView();
        }

        return midpProfileView;
    }
}
